import type {
  Oauth2ThirdClientDtlVo,
  Oauth2ThirdClientListVo,
  Oauth2ThirdClientPageQo,
  Oauth2ThirdClientQo,
  Oauth2ThirdClientVo,
} from '#/oauth/thirdClient'
import { http } from '@/utils/request/index'

/**
 * 获取第三方客户端分页列表
 * @param params 查询参数
 * @returns 第三方客户端列表
 */
export function getOauth2ThirdClientPageListApi(params: Oauth2ThirdClientPageQo) {
  return http.request<PageResult<Oauth2ThirdClientVo>>('get', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientPage', {
    params,
  })
}

/**
 * 获取第三方客户端列表
 * @returns 第三方客户端列表
 */
export async function getOauth2ThirdClientListApi() {
  const res = await http.request<Oauth2ThirdClientListVo[]>('get', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientList')

  res.data.forEach((item) => {
    // GItee拼接授权地址
    if (item.code === 'GITEE') {
      item._authorizationUri = `${item.authorizationUri}?client_id=${item.clientId}&redirect_uri=${encodeURIComponent(item.pcRedirectUrl ?? '')}&response_type=code`
    }
  })

  return res
}

/**
 * 获取第三方客户端详情
 * @param id 第三方客户端id
 * @returns 第三方客户端详情
 */
export function getOauth2ThirdClientDetailsApi(id?: string) {
  return http.request<Oauth2ThirdClientDtlVo>('get', `/admin-api/system/oauth2ThirdClient/oauth2ThirdClientDetails/${id}`)
}

/**
 * 新增第三方客户端
 * @param data 第三方客户端信息
 * @returns 第三方客户端id
 */
export function createOauth2ThirdClientApi(data: Oauth2ThirdClientQo) {
  return http.request<string>('post', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientCreate', {
    data,
  })
}

/**
 * 修改第三方客户端
 * @param data 第三方客户端信息
 * @returns 第三方客户端id
 */
export function updateOauth2ThirdClientApi(data: Oauth2ThirdClientQo) {
  return http.request<string>('post', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientUpdate', {
    data,
  })
}

/**
 * 删除第三方客户端
 * @param idList 第三方客户端id列表
 * @returns 第三方客户端id
 */
export function deleteOauth2ThirdClientApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientRemove', {
    params: {
      idList: idList.join(','),
    },
  })
}

/**
 * 启用第三方客户端
 * @param idList 第三方客户端id列表
 * @returns 第三方客户端id
 */
export function enableOauth2ThirdClientApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientEnable', {
    params: {
      idList: idList.join(','),
    },
  })
}

/**
 * 禁用第三方客户端
 * @param idList 第三方客户端id列表
 * @returns 第三方客户端id
 */
export function disableOauth2ThirdClientApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/oauth2ThirdClient/oauth2ThirdClientDisable', {
    params: {
      idList: idList.join(','),
    },
  })
}
